package zhaomh.zlogin;

import org.bukkit.Server;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import zhaomh.zlogin.Zlogin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class Tools {
    public static final Zlogin PLUGIN = getPlugin();
    public static String PLUGIN_NAME;
    private Tools(){}
    public static void initialize(String string) {setPluginName(string);}
    public static void setPluginName(String string){
        PLUGIN_NAME = colorReplace(string);
    }
    public static Zlogin getPlugin(){
        return Zlogin.getPlugin(Zlogin.class);
    }
    public static String colorReplace(String string) {
        if (string == null)
            return null;
        return string.replace("&","§");

    }
    public static Server getServer() {
        return PLUGIN.getServer();
    }
    public static void sendServerMessage(String string) {
        getServer().broadcastMessage(colorReplace(string));
    }
    public static List<String> getPlayerNames(){
        List<String> list = new ArrayList<>();
        List<? extends Player> players;
        players = getServer().getOnlinePlayers().stream().toList();
        for (Player player : players) {
            list.add(player.getName());
        }
        return list;
    }
    public static int getRandom(int min, int max) {
        return ThreadLocalRandom.current().nextInt(min,max);
    }
    public static List<String> getKeysAtPath(YamlConfiguration config, String path) {
        List<String> keys = new ArrayList<>();

        // 获取指定路径下的配置内容
        ConfigurationSection section = config.getConfigurationSection(path);

        // 如果路径下的内容是一个Section，则获取其keys
        if (section != null) {
            // 获取section的直接子keys
            keys.addAll(section.getKeys(false));
        }

        return keys;
    }
}
